package com.xnf.ssmweb.dao;

import com.sun.org.glassfish.external.statistics.annotations.Reset;
import com.xnf.ssmweb.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by chendx on 2021/3/30.
 */
public interface RoleMapper {

    @Select("select * from role where id in (select roleId from users_role where userId = #{userId})")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(column = "id" , property = "permissions" ,javaType = List.class,
            many = @Many(select = "com.xnf.ssmweb.dao.PermissionMapper.findPermissionByRoleId"))
    })
    List<Role> findRoleByUserId(Long userId);


    @Select("select * from role where id in (select roleId from role_permission where permissionId = #{permissionId})")
    List<Role> findRoleByPermissionId(Long permissionId);

    @Select("select * from role")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(column = "id", property = "userInfos", javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.UserInfoMapper.findUserByRoleId")),
            @Result(column = "id", property = "permissions", javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.PermissionMapper.findPermissionByRoleId")),
    })
    List<Role> findAll();

    @Select("select * from role where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(column = "id", property = "userInfos", javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.UserInfoMapper.findUserByRoleId")),
            @Result(column = "id", property = "permissions", javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.PermissionMapper.findPermissionByRoleId")),
    })
    Role findById(Long id);

    @Insert("insert into role (roleName,roleDesc) values (#{roleName},#{roleDesc})")
    int save(Role role);

    @Insert("insert into role_permission (roleId,permissionId) values (#{roleId},#{permissionId})")
    void savePermissionToRole(@Param("roleId") Long roleId,@Param("permissionId") Long permissionId);

    @Select("select * from role where id not in ( select roleId from users_role where userId = #{userId})")
    List<Role> findNoRoleList(@Param("userId") Long userId);
}
